#define _CRT_SECURE_NO_WARNINGS

#include "Queue.h"


void QueueInit(Queue* q)
{
	assert(q);
	q->_front = q->_rear = (Queue*)malloc(sizeof(Queue));
	if (q->_front == NULL)
	{
		perror("malloc fail");
		return;
	}
	q->_front->_pNext = q->_rear->_pNext = NULL;

}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->_data = data;
	newnode->_pNext = NULL;
	q->_rear->_pNext = newnode;
	q->_rear = newnode;
}

void QueuePop(Queue* q)
{
	assert(q);

	QNode* del = q->_front;
	q->_front = q->_front->_pNext;
	free(del);
	del = NULL;
}

QDataType QueueFront(Queue* q)
{
	assert(q);

	return q->_front->_pNext->_data;
}

QDataType QueueBack(Queue* q)
{
	assert(q);

	return q->_rear->_data;
}

int QueueSize(Queue* q)
{
	assert(q);

	int count = 0;
	QNode* pur = q->_front->_pNext;
	while (pur)
	{
		pur = pur->_pNext;
		count++;
	}
	return count;
}

int QueueEmpty(Queue* q)
{
	assert(q);

	return q->_front == q->_rear;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	while (q->_front != q->_rear)
	{
		QNode* del = q->_front;
		q->_front = q->_front->_pNext;
		free(del);
		del = NULL;
	}
}